package com.dailyblue.java.cloud.gateway.filter;

import com.dailyblue.java.cloud.gateway.config.ParamsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class LoginGatewayFilterFactory extends AbstractGatewayFilterFactory<String> {
    public LoginGatewayFilterFactory() {
        super(String.class);
    }

    @Override
    public GatewayFilter apply(String string) {
        GatewayFilter gf = new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                String username = exchange.getRequest().getQueryParams().getFirst("username");
                String password = exchange.getRequest().getQueryParams().getFirst("password");
                log.info("接收的账号：{},密码：{}", username, password);
                return chain.filter(exchange);
            }
        };
        return gf;
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("username", "password");
    }
}
